package STUDENT.TRINHQUANGXUANDUC.OPPS.QuanLyNhanSu.Models;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import STUDENT.TRINHQUANGXUANDUC.OPPS.QuanLyNhanSu.Entry.Chamcong;
import STUDENT.TRINHQUANGXUANDUC.OPPS.QuanLyNhanSu.Entry.Nhanvien;
import STUDENT.TRINHQUANGXUANDUC.OPPS.QuanLyNhanSu.Entry.Phongban;

public class Models {
	private List<Nhanvien> listNhanviens = new ArrayList<Nhanvien>();
	private List<Phongban> listPhongbans = new ArrayList<Phongban>();
	private List<Chamcong> lChamcongs = new ArrayList<Chamcong>();
	ObjectMapper objMapper;

	/***
	 * phuong thuc nhap nhan vien moi
	 * 
	 * @param objNhanvien
	 * @return
	 */

	public List<Nhanvien> nhapNhanviens(Nhanvien objNhanvien) throws IOException, NumberFormatException {
		listNhanviens.add(objNhanvien);
		writeData(1);
		return listNhanviens;
	}

	/**
	 * Phuong thuc nhap phong ban moi
	 * 
	 * @param objPhongban
	 * @return
	 */

	public List<Phongban> nhapPhongbans(Phongban objPhongban) {
		listPhongbans.add(objPhongban);
		try {
			writeData(2);
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listPhongbans;
	}

	/***
	 * Phuong thuc nhap bang cham cong cho nhan vien
	 * 
	 * @param objChamcong
	 * @return
	 */
	public List<Chamcong> nhapChamcongs(Chamcong objChamcong) {
		lChamcongs.add(objChamcong);
		try {
			writeData(3);
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lChamcongs;
	}

	/**
	 * phuong thuc lay danh sach cham cong
	 * 
	 * @return
	 */
	public String getListchamcong() {
		String danhsach = "";
		for (Chamcong chamcong : lChamcongs) {
			danhsach += "Ma nhan vien: " + chamcong.getNhanvien().getIdnhanvien() + "\n\r" + "Ten nhan vien: "
					+ chamcong.getNhanvien().getHoten() + "\n\r" + "Phong ban: " + chamcong.getPhongban().getTenphong()
					+ "\n\r" + "So ngay lam viec: " + chamcong.getSongaylamviec() + "\n\r" + "************************"
					+ "\n\r";
		}
		return danhsach;
	}

	public String getLIstNhanvien() {
		String danhsach = "";
		for (Nhanvien nhanvien : listNhanviens) {
			danhsach += "Ma nhan vien: " + nhanvien.getIdnhanvien() + "\n\r" + "hoten:" + nhanvien.getHoten() + "\n\r"
					+ "Dia chi: " + nhanvien.getDiachi() + "\n\r" + "So dien thoai: " + nhanvien.getSodienthoai()
					+ "\n\r" + "Bac luong: " + nhanvien.getBacluong() + "\n\r" + "***************************" + "\n\r";
		}
		return danhsach;
	}

	public String getPhongban() {
		String phongban = "";
		for (Phongban pb : listPhongbans) {
			phongban += "Ma phong ban: " + pb.getIdphongban() + "\n\r" + "Ten phong ban: " + pb.getTenphong() + "\n\r"
					+ "He so cong viec: " + pb.getHesocongviec() + "\n\r" + "Mo ta: " + pb.getMota() + "\n\r"
					+ "**********************" + "\n\r";
		}
		return phongban;
	}

	public String thongKe() {
		TreeMap<String, Integer> thunhap = new TreeMap<String, Integer>();
		String ds = "";
		for (int j = 0; j < lChamcongs.size(); j++) {
			String hoten = lChamcongs.get(j).getNhanvien().getHoten();
			for (int i = j + 1; i < lChamcongs.size(); i++) {
				int tongtien = 0;
				if (lChamcongs.get(i).getNhanvien().getIdnhanvien() == lChamcongs.get(j).getNhanvien()
						.getIdnhanvien()) {
					int tien1 = 85000 * (2 + lChamcongs.get(i).getNhanvien().getBacluong() / 3)
							* lChamcongs.get(i).getPhongban().getHesocongviec() * lChamcongs.get(i).getSongaylamviec()
							/ 22;
					int tien2 = 85000 * (2 + lChamcongs.get(j).getNhanvien().getBacluong() / 3)
							* lChamcongs.get(j).getPhongban().getHesocongviec() * lChamcongs.get(j).getSongaylamviec()
							/ 22;
					tongtien = tien1 + tien2;
					thunhap.put(hoten, tongtien);
				} else {
					int tien = 85000 * (2 + lChamcongs.get(i).getNhanvien().getBacluong() / 3)
							* lChamcongs.get(i).getPhongban().getHesocongviec() * lChamcongs.get(i).getSongaylamviec()
							/ 22;
					thunhap.put(hoten, tien);
				}
			}
		}
		Set set = thunhap.entrySet();
		Iterator i = set.iterator();
		while (i.hasNext()) {
			Map.Entry me = (Map.Entry) i.next();
			ds += "Ho ten: " + me.getKey() + " - Thu nhap: " + me.getValue() + " VND" + "\n\r"
					+ "***************************" + "\n\r";

		}
		return ds;
	}

	public List<Nhanvien> getdanhsachNhanvien() {
		return this.listNhanviens;
	}

	public List<Phongban> getdanhsachPhongban() {
		return this.listPhongbans;
	}

	public void writeData(int opt) throws IOException, JsonMappingException, JsonGenerationException {
		objMapper = new ObjectMapper();
		if (opt == 1) {
			objMapper.writeValue(new File("C:\\nhanvien.json"), listNhanviens);
		}
		if (opt == 2) {
			objMapper.writeValue(new File("C:\\phongban.json"), listPhongbans);
		}
		if (opt == 3) {
			objMapper.writeValue(new File("C:\\chamcong.json"), lChamcongs);
		}
	}

	public void loadData() throws IOException, JsonGenerationException, JsonMappingException, JsonMappingException {

		objMapper = new ObjectMapper();

		listNhanviens = objMapper.readValue(new File("C:\\nhanvien.json"), new TypeReference<List<Nhanvien>>() {
		});
		listPhongbans = objMapper.readValue(new File("C:\\phongban.json"), new TypeReference<List<Phongban>>() {
		});

		List<Chamcong> temp = objMapper.readValue(new File("C:\\chamcong.json"), new TypeReference<List<Chamcong>>() {
		});
		for (Chamcong cc : temp) {
			lChamcongs.add(cc);
		}

	}

}
